package com.yunhe.common.metadata;

import com.alibaba.excel.metadata.BaseRowModel;
import com.yunhe.common.util.ReflectUtil;
import com.yunhe.common.util.StringUtil;

import java.lang.reflect.Field;

/**
 * @Author: DongWenYu
 * @Date: 2021/4/28 10:34
 */
public class BaseExcelModel extends BaseRowModel {

    //判断上传上来的当前行是否为空,默认有一个不为空就不是空，子类重写。
    public Boolean isEmpty(){
        Class<? extends BaseExcelModel> clazz = this.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields){
            if (!StringUtil.isEmpty(ReflectUtil.getFieldValue(field, this))) {
                return false;
            }
        }
        return true;
    }

    //同一个list中出现相同数据，后面的数据覆盖前面的数据的更新方法
    public void update(BaseExcelModel that){
        Class thisClass = this.getClass();
        Class thatClass = that.getClass();

        Field[] thisFields = thisClass.getDeclaredFields();
        Field[] thatFields = thatClass.getDeclaredFields();
        for (int i = 0 ; i < thisFields.length ; i ++){
            Field thisField = thisFields[i];
            Field thatField = thatFields[i];
            String thatValue = ReflectUtil.getFieldValue(thatField, that);
            if ("serialVersionUID".equals(thisField.getName().toString()) && !StringUtil.isEmpty(thatValue) ){
                ReflectUtil.setFieldValue(thisField,thatValue,this);
            }
        }
    }

    public void check(){
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields){
            String fieldValue = ReflectUtil.getFieldValue(field, this);
            if (fieldValue == null || "-".equals(fieldValue) || "".equals(fieldValue) || "/".equals(fieldValue)){
                ReflectUtil.setFieldValue(field,null,this);
            }
        }
    }

    private static Boolean isNull(Field enterpriseUploadField , BaseExcelModel enterpriseUploadAQC) throws IllegalAccessException {
        if( (enterpriseUploadField.get(enterpriseUploadAQC) == null &&  !"serialVersionUID".equals(enterpriseUploadField.getName().toString()))
                || (StringUtil.isEmpty(enterpriseUploadField.get(enterpriseUploadAQC).toString())  &&  !"serialVersionUID".equals(enterpriseUploadField.getName().toString()))){
            return true;
        }
        else return false;
    }
}
